/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stendcontrol;

import gui.ArrayListModel;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author minaev
 */
public class SaveLoadExpContainer {

    private ArrayListModel<SaveLoadExp> saveloaders = new ArrayListModel<SaveLoadExp>();
    private ArrayList<Integer> cnt_exps = new ArrayList<Integer>();

    public void init() {
        this.add(new FileServer());
        try {
            this.add(new DBServer());
        } catch (SQLException ex) {
            StendLogger.log(StendLogger.MSG_ERROR, "Can`t create DB source, SQL"
                    + " exception: " + ex.getMessage());
        } catch (ClassNotFoundException ex) {
            StendLogger.log(StendLogger.MSG_ERROR, "Can`t create DB source, SQL "
                    + "driver exception: " + ex.getMessage());
        }
    }

    public synchronized void add(SaveLoadExp sle) {
        saveloaders.add(sle);
        cnt_exps.add(sle.getExpCount());
    }

    public synchronized SaveLoadExp get(int nsle) {
        return saveloaders.get(nsle);
    }

    public int getCount(int nsle) {
        return cnt_exps.get(nsle).intValue();
    }

    public synchronized void remove(int nsle) {
        saveloaders.remove(nsle);
        cnt_exps.remove(nsle);
    }

    public synchronized void remove(SaveLoadExp sle) {
        if (sle != null) {
            if (saveloaders.contains(sle)) {
                cnt_exps.remove(saveloaders.indexOf(sle));
                saveloaders.remove(sle);
            }
        }
    }

    public synchronized void removeAll() {
        saveloaders.clear();
        cnt_exps.clear();
    }

    public synchronized String[] getSaveLoadExpNames() {
        String[] rstr = new String[saveloaders.size()];
        for (int i = 0; i < saveloaders.size(); i++) {
            rstr[i] = saveloaders.get(i).getNameSaver();
        }
        return rstr;
    }

    public synchronized String[] getNamesExps() {
        String[] rstr = new String[0];
        String[] fbuf;
        String[] sbuf;
        for (int i = 0; i < saveloaders.size(); i++) {
            fbuf = rstr;
            sbuf = saveloaders.get(i).getNamesExp();
            cnt_exps.set(i, sbuf.length);
            rstr = new String[fbuf.length + sbuf.length];
            System.arraycopy(fbuf, 0, rstr, 0, fbuf.length);
            System.arraycopy(sbuf, 0, rstr, fbuf.length, sbuf.length);
        }
        return rstr;
    }

    public synchronized Experiment loadExp(int nexp) {
        int i = 0;
        while (nexp >= cnt_exps.get(i).intValue()) {
            nexp -= cnt_exps.get(i).intValue();
            i++;
        }
        return saveloaders.get(i).loadExp(nexp);
    }

    public synchronized Experiment loadExp(int nsourse, int nexp) {
        return saveloaders.get(nsourse).loadExp(nexp);
    }

    public synchronized void saveExp(Experiment exp, int n_saveloader) {
        saveloaders.get(n_saveloader).saveExp(exp);
        cnt_exps.set(n_saveloader, saveloaders.get(n_saveloader).getExpCount());
    }

    public ArrayListModel<SaveLoadExp> getSaveLoaders() {
        return saveloaders;
    }
}
